package cn.hfatec.shms.system.codeInfo.service;

import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.system.codeInfo.dao.CodeInfoDao;
import cn.hfatec.shms.system.codeInfo.model.CodeInfo;
import cn.hfatec.shms.system.codeInfo.model.CodeInfoParams;
import com.google.common.collect.Lists;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @author hfatec
 * @version 1.0
 *          <p>
 *          the JAVA code is generate by middlegen
 */
@Service("CodeInfoService")
public class CodeInfoService {

    //注入Dao层
    @Resource(name = "CodeInfoDao")
    private CodeInfoDao codeInfoDao;

    /**
     * 查询数据
     *
     * @param codeTypeIds
     */
    @Transactional
    public List queryCodeInfoTable(String[] codeTypeIds) {
        if (codeTypeIds == null || codeTypeIds.length == 0) {
            return Lists.newArrayList();
        }

        return codeInfoDao.queryCodeInfoByTpyeIds(codeTypeIds);
    }

    /**
     * 插入数据
     *
     * @param codeInfo
     */
    @Transactional
    public void saveCodeInfo(CodeInfo codeInfo) {
        if (codeInfo.getId() != null) {
            CodeInfo result = getCodeInfo(codeInfo.getId());
            if (result != null) {
                codeInfoDao.update(codeInfo);
            } else {
                codeInfoDao.insert(codeInfo);
            }
        } else {
            codeInfoDao.insert(codeInfo);
        }
        CodeInfoCache.refreshCache(codeInfoDao);
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    public void deleteCodeInfo(Integer id) {

        codeInfoDao.delete(id);
        CodeInfoCache.refreshCache(codeInfoDao);
    }

    /**
     * 删除数据
     *
     * @param codeInfoList
     */
    @Transactional
    public void deleteCodeInfoBatch(List<String> codeInfoList) {
        for (String id : codeInfoList) {
            deleteCodeInfo(Integer.valueOf(id));
        }
        CodeInfoCache.refreshCache(codeInfoDao);
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    public CodeInfo getCodeInfo(Integer id) {

        return codeInfoDao.getCodeInfoById(id);
    }

    /**
     * 查询数据
     *
     * @param codeTypeId
     */
    @Transactional
    public List queryCodeInfoList(String codeTypeId) {
        if (codeTypeId == null) {
            return Lists.newArrayList();
        }
        CodeInfoParams codeInfoParams = new CodeInfoParams();
        codeInfoParams.setCodeTypeId(codeTypeId);
        return codeInfoDao.queryCodeInfo(codeInfoParams);
    }

    /**
     * 查询数据
     */
    @Transactional
    public List getCodeTypeList() {
        CodeType[] codeTypes = CodeType.values();
        List codeTypeList = new ArrayList();
        for (int i = 0; i < codeTypes.length; i++) {
            CodeInfo codeInfo = new CodeInfo();
            codeInfo.setValue(codeTypes[i].name());
            codeInfo.setContent(codeTypes[i].getDesc());
            codeTypeList.add(codeInfo);
        }
        return codeTypeList;
    }

    /**
     * 查询数据
     *
     * @param codeInfoParams
     */
    @Transactional
    public List queryCodeInfoList(CodeInfoParams codeInfoParams) {

        return codeInfoDao.queryCodeInfo(codeInfoParams);
    }

    /**
     * 根据代码类型、上级代码值，获取代码信息列表
     *
     * @param codeType
     * @param parentValue
     * @return
     */
    public List getCodeInfo(CodeType codeType, String parentValue) {

        List<CodeInfo> list = getCodeInfo(codeType);

        List result = Lists.newArrayList();
        for (CodeInfo codeInfo : list) {
            if (codeInfo.getParentValue().equals(parentValue)) {
                result.add(codeInfo);
            }
        }
        return result;
    }

    /**
     * 获取代码信息列表
     *
     * @param codeType
     * @return
     */
    public List<CodeInfo> getCodeInfo(CodeType codeType) {
        CodeInfoParams codeInfoParams = new CodeInfoParams();
        codeInfoParams.setCodeTypeId(codeType.name());
        return codeInfoDao.queryCodeInfo(codeInfoParams);
    }
}